﻿using Domain.Entitys;
using Domain.Repositorys;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class EMRRepository : IEMRRepositoty
    {
        private readonly MyDbContext myDbContext;

        public EMRRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public async Task<int> EMRAddAsync(EMR eMR)
        {
            await myDbContext.EMRs.AddAsync(eMR);
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<int> EMRDeleteAsync(int id)
        {
            EMR emr = myDbContext.EMRs.Find(id);
            myDbContext.EMRs.Remove(emr);
            return await myDbContext.SaveChangesAsync() ;   
        }

        public int EMRSelectCount()
        {
            return myDbContext.EMRs.Count();
        }

        public async Task<List<EMR>> EMRSelectFenye(int pageIndex, int pageSize)
        {
            return await myDbContext.EMRs.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public  Task<EMR> EMRSelectId(int id)
        {
            return  myDbContext.EMRs.FirstOrDefaultAsync(c => c.MreId == id);
        }

        public async Task<int> EMRUpdateAsync(EMR emr)
        {
            EMR emrrr = await myDbContext.EMRs.FirstOrDefaultAsync(e => e.MreId == emr.MreId);
            emrrr.ChangeTime = emr.ChangeTime;
            emrrr.Changer = emr.Changer;
            emrrr.Deadline = emr.Deadline;
            emrrr.EngageRequired = emr.EngageRequired;
            emrrr.EngageType = emr.EngageType;
            emrrr.HumanAmount = emr.HumanAmount;
            emrrr.MajorDescribe = emr.MajorDescribe;
            myDbContext.EMRs.Update(emrrr);
            return await myDbContext.SaveChangesAsync();
            
        }
    }
}
